#define _CRT_SECURE_NO_WARNINGS 1

#include"string.h";

namespace yyh
{
	const size_t string::npos = -1;

	string::string(const char* str)
		:_size(strlen(str))
	{
		_str = new char[_size + 1];
		_capacity = _size;
		strcpy(_str, str);	
	}

	string::string(const string& s)
	{
		string temp(s._str);
		swap(temp);
	}

	string& string::operator =(const string& s)
	{
		if (this != &s)
		{
			delete[] _str;
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}
		return *this;
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* temp = new char[n + 1];
			strcpy(temp, _str);
			delete[] _str;
			_str = temp;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		_str[_size++] = ch;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t n = strlen(str);
		if (_size + n > _capacity)
		{
			size_t NewCapacity = 2 * _capacity;
			if (_size + n > NewCapacity)
			{
				NewCapacity = _size + n;
			}
			reserve(NewCapacity);
		}
		strcpy(_str + _size, str);
		_size += n;
	}

	string& string::operator +=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator +=(const char* str)
	{
		append(str);
		return *this;
	}

	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}

	void string::resize(size_t n, char ch)
	{
		if (n > _size)
		{
			if (n > _capacity)
			{
				reserve(n);
			}
			memset(_str + _size, ch, n - _size);
		}
		_size = n;
		_str[_size] = '\0';
	}

	char& string::operator [](size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}

	const char& string::operator [](size_t pos) const
	{
		assert(pos < _size);
		return _str[pos];
	}

	bool string::operator ==(const string& s) const
	{
		return strcmp(_str, s._str) == 0;
	}

	bool string::operator !=(const string& s) const
	{
		return !(*this == s);
	}

	bool string::operator <(const string& s) const
	{
		return strcmp(_str, s._str) < 0;
	}

	bool string::operator <=(const string& s) const
	{
		return *this < s || *this == s;
	}

	bool string::operator >(const string& s) const
	{
		return !(*this <= s);
	}

	bool string::operator >=(const string& s) const
	{
		return !(*this < s);
	}

	size_t string::find(char c, size_t pos)
	{
		for (size_t i = 0; i < _size; i++)
		{
			if (_str[i] == c)
			{
				return i;
			}
		}
		return npos;
	}

	size_t string::find(const char* str, size_t pos)
	{
		const char* p = strstr(_str, str);
		if (p == nullptr)
		{
			return npos;
		}
		else
		{
			return p - _str;
		}
	}

	void string::insert(size_t pos, size_t n, char ch)
	{
		assert(pos <= _size);
		assert(n > 0);
		if (_size + n > _capacity)
		{
			size_t NewCapacity = 2 * _capacity;
			if (_size + n > NewCapacity)
			{
				NewCapacity = _size + n;
			}
			reserve(NewCapacity);
		}
		size_t end = _size + n;
		while (end > pos + n - 1)
		{
			_str[end] = _str[end - n];
			end--;
		}
		for (size_t i = 0; i < n; i++)
		{
			_str[pos + i] = ch;
		}
		_size += n;
	}

	void string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		size_t n = strlen(str);
		insert(pos, n, 'x');
		for (size_t i = 0; i < n; i++)
		{
			_str[pos + i] = str[i];
		}
	}

	void string::erase(size_t pos, size_t len)
	{
		if (len >= _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			size_t end = pos + len;
			while (end <= _size)
			{
				_str[end - len] = _str[end];
				end++;
			}
			_size -= len;
		}
	}

	void test1()
	{
		string s1;
		cout << s1.c_str() << endl;

		string s2("hello world");
		cout << s2.c_str() << endl;

		string s3(s2);
		cout << s3.c_str() << endl;

		string s4("yyh");
		s4 = s3;
		cout << s4.c_str() << endl;

		string::iterator it = s2.begin();
		while (it != s2.end())
		{
			cout << *it << ' ';
			it++;
		}
		cout << endl;

		for (auto e : s2)
		{
			cout << e << ' ';
		}
		cout << endl;
	}

	void test2()
	{
		string s1("hello world");
		s1 += 'x';
		s1 += "yyh";
		s1 += "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
		cout << s1.c_str() << endl;

		s1.clear();
		cout << s1.c_str() << endl;

		string s2("yyh");
		s2.resize(10, 'x');
		cout << s2.c_str() << endl;
		s2.resize(3);
		cout << s2.c_str() << endl;

		for (size_t i = 0; i < s2.size(); i++)
		{
			cout << s2[i] << ' ';
		}
		cout << endl;

		string s3("123456789");
		cout << s3.find('5') << endl;
		cout << s3.find("789") << endl;
	}

	void test3()
	{
		string s1("hello world");
		s1.insert(11, 3, 'x');
		cout << s1.c_str() << endl;
		s1.insert(6, 3, 'x');
		cout << s1.c_str() << endl;
		s1.insert(0, 3, 'x');
		cout << s1.c_str() << endl;

		string s2("hello world");
		s2.insert(11, "xxx");
		cout << s2.c_str() << endl;
		s2.insert(6, "xxx");
		cout << s2.c_str() << endl;
		s2.insert(0, "xxx");
		cout << s2.c_str() << endl;

		string s3("hello world");
		cout << s3.c_str() << endl;
		s3.erase(6, 3);
		cout << s3.c_str() << endl;
		s3.erase(0, 5);
		cout << s3.c_str() << endl;
	}
}